import httpInstance from '@/utils/http'
// 创建订单
export const createOrderAPI = async (data) => {
  try {
    const res = await httpInstance.request({
      url: `/api/orders/create`,
      method: 'POST',
      data: {
        products : data
      }
    })

    return res
  } catch (error) {
    throw error;
  }
}
/**
 * 获取订单信息
 */
export const getCheckoutInfoAPI = async (orderId) => {
  try {
    const res = await httpInstance.request({
      url: `/api/orders/${orderId}`
    })

    return res;
  } catch (error) {
    throw error;
  }
}


// 完善订单
export const completeOrderAPI = async ({orderNo,addressId}) => {
  try {
    const res = await httpInstance.request({
      url: `/api/orders/complete`,
      method: 'PUT',
      data: {
        orderNo,
        addressId
      }
    })

    return res
  } catch (error) {
    throw error;
  }
}

// 取消订单
export const cancelOrderAPI = async (orderId) => {
  try {
    const res = await httpInstance.request({
      url: `/api/orders/cancel/${orderId}`,
      method: 'PUT'
    })

    return res
  } catch (error) {
    throw error;
  }
}

//获取支付信息
export const getPayInfoAPI = async (orderId) => {
  try {
    const res = await httpInstance.request({
      url: `/api/orders/pay/${orderId}`,
      method: 'GET',
    })

    return res
  } catch (error) {
    throw error;
  }
}

//支付订单
export const payOrderAPI = async ({orderNo,payType,isPay}) => {
  try {
    const res = await httpInstance.request({
      url: `/api/orders/pay`,
      method: 'PUT',
      data: {
        orderNo,
        payType,
        isPay
      }
    })

    return res
  }
  catch (error) {
    throw error;
  }
}

// 获取订单列表
export const getOrderListAPI = async (size = 1000, current = 2) => {
  try {
    const res = await httpInstance.request({
      url: `/api/orders`,
      method: 'POST',
      data: {
        size,
        current
      }
    });

    return res;
  } catch (error) {
    throw error;
  }
};
